terraform {
  required_version = "~> 1.4.5"

  required_providers {
    maas = {
      source  = "terraform.local/local/maas"
      version = "1.1.1"
    }
  }
}

resource "maas_machine" "main" {
  hostname   = var.machine_hostname
  power_type = "ipmi"
  power_parameters = {
    power_address = var.ipmi_power_address
    power_user    = var.ipmi_power_user
    power_pass    = var.ipmi_power_pass
    power_boot_type = "efi"
    power_driver    = "LAN_2_0"
    privilege_level = var.ipmi_privilege_level
  }
  domain = var.machine_domain
  pool = var.machine_pool
  timeouts {
    create = "60m"
  }   
}

data "maas_subnet" "pxe" {
  cidr = var.machine_cidr
}

data "maas_machine" "main" {
  system_id = maas_machine.main.id
}

data "maas_fabric" "default" {
  name = "fabric-0"
}

data "maas_vlan" "default" {
  fabric = data.maas_fabric.default.id
  vlan = var.maas_default_vlan
}

data "maas_fabric" "custom" {
  name = "custom-fabric"
}

data "maas_vlan" "custom" {
  fabric = data.maas_fabric.custom.id
  vlan = var.maas_custom_vlan
}

locals {
  interfaces = [
    for nic in data.maas_machine.main.interfaces :
    nic if nic.type == "physical" && nic.firmware_version != "CDC Ethernet Device" && nic.interface_speed == 25000 && nic.link_speed == 25000
  ]
}

locals {
  offline_interfaces = [
    for nic in data.maas_machine.main.interfaces :
    nic if nic.type == "physical" && nic.link_speed == 0
  ]
}

resource "maas_network_interface_physical" "main_nic0" {
  machine = maas_machine.main.id
  vlan    = data.maas_vlan.default.id
  mtu     = var.machine_network_mtu

  mac_address = local.interfaces.0.mac_address
}

resource "maas_network_interface_physical" "main_nic1" {
  machine = maas_machine.main.id
  vlan    = data.maas_vlan.default.id
  mtu     = var.machine_network_mtu

  mac_address = local.interfaces.1.mac_address
}

resource "maas_network_interface_physical" "offline_nic" {
  machine = maas_machine.main.id
  vlan    = data.maas_vlan.custom.id

  for_each    = toset([for nic in local.offline_interfaces : nic.mac_address])
  mac_address = each.value
  depends_on = [maas_network_interface_bond.main_bond4]
}

resource "maas_network_interface_bond" "main_bond4" {
  machine               = maas_machine.main.id
  name                  = "bond4"
  bond_miimon           = var.bond_miimon
  bond_mode             = "802.3ad"
  bond_xmit_hash_policy = var.bond_xmit_hash_policy
  mac_address           = local.interfaces.0.mac_address
  vlan                  = data.maas_vlan.default.id
  parents               = [for nic in local.interfaces : nic.id]

#   depends_on = [maas_network_interface_physical.main_nic]
  depends_on = [maas_network_interface_physical.main_nic0, maas_network_interface_physical.main_nic1]
}

resource "maas_network_interface_link" "main_bond4" {
  machine           = maas_machine.main.id
  network_interface = maas_network_interface_bond.main_bond4.id
  subnet            = data.maas_subnet.pxe.id
  mode              = "STATIC"
  ip_address        = var.machine_ip_address
  default_gateway   = true

  depends_on = [maas_network_interface_bond.main_bond4]
}

# resource "maas_block_device" "vda" {
#   count = var.is_kube_worker ? 1 : 0
#   machine = maas_machine.main.id
#   name = "sda"
#   id_path = "/dev/sda"
#   is_boot_device = true
#   size_gigabytes = 15
# }

resource "maas_instance" "main" {
  count = var.enabled ? 1 : 0    # conditional creation of a resource
  allocate_params {
    hostname = maas_machine.main.hostname
  }
  deploy_params {
    distro_series = var.instance_image
  }
  depends_on = [maas_network_interface_link.main_bond4]
  timeouts {
    create = "60m"
  }  
}
